/*
Copyright (C) Ariff Ehsan, Abdul Hadi Fikri

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/*  Author : Abdul Hadi Fikri
 *  Date   : June 24, 2009
 */

#include <system.h>
#include <button/button0.h>

/**
*CODE FUNCTIONALITY
*
*--Function name called by user of this module is in section "Provides", "Uses" is for any function 
*--name this module need to call in turn to signaled an event for the user.
*
*Interface button0{
*	Provides{
*		command BUTTON0_init();
*	}
*	Uses{
*		signal BUTTON0_pushed();
*		signal BUTTON0_released();
*	}
*}
*
*--Component in call section are function name which this module use from other module in order to complete its
*--tasks, event in turn is any event siganalled by the other module(s).  
*
*Component{
*	call{
*		TIMER-> TIMER_reg();
*		TIMER-> TIMER_startPeriodic();
*		TIMER-> TIMER_stop();
*		TASK -> TASK_reg();
*		TASK -> TASK_post();
*	}
*	event{
*		APP_TIMER_BUTTON0_expired() <- TIMER
*		APP_TASK_BUTTON0_run()      <- TASK
*	}
*}
*
**
*Data Field:
*/
/*Button status enumeration*/
enum{
	BS0_PUSHED = 0,
	BS0_PUSHING,
	BS0_RELEASED,
	BS0_RELEASING,
};

/*Button status variable*/
uint8_t app_button0_state;

/*Timer instantiation*/
timer_t app_timer_button0;

/*Task instantiation*/
task_t app_task_button0;

/**
*Function Field:
*/void APP_TIMER_BUTTON0_expired(void);
  void APP_TASK_BUTTON0_run(void);

/********************************************************************************************/
/*                                  Clients/Command Call                                    */
/********************************************************************************************/
void BUTTON0_init(){
	/*Initialize button status*/
	app_button0_state = BS0_RELEASED;
	
	/*Configure PB0 as input pin*/
	CLR_BIT(PORTB,PB0);

	
	/*Register timer and task*/
	TASK_reg(&app_task_button0,APP_TASK_BUTTON0_run);
	TIMER_reg(&app_timer_button0,APP_TIMER_BUTTON0_expired);
	
	/*Start polling button status*/
	TASK_post(&app_task_button0);	
}
/********************************************************************************************/
/*                              HW/SW Interrupt/Event Signalled                             */
/********************************************************************************************/
void APP_TIMER_BUTTON0_expired(void){
	switch(app_button0_state){
		case BS0_RELEASING:
			app_button0_state = BS0_RELEASED;
			BUTTON0_released();
		break;
		case BS0_PUSHING:
			app_button0_state = BS0_PUSHED;
			BUTTON0_pushed();
		break;
	}
	TASK_post(&app_task_button0);
}

void APP_TASK_BUTTON0_run(void){
	switch(app_button0_state){
		case BS0_RELEASED:
			if(READ_BIT(PINB,PB0)==0){
				app_button0_state = BS0_PUSHING;
				TIMER_start(&app_timer_button0,20);
			}
			else{
				TASK_post(&app_task_button0);
			}
		break;
		case BS0_PUSHED:
			if(READ_BIT(PINB,PB0)==1){
				app_button0_state = BS0_RELEASING;
				TIMER_start(&app_timer_button0,20);
			}
			else{
				TASK_post(&app_task_button0);
			}
		break;
	}
}
